package com.zero.common.domain;

import cn.hutool.core.util.ObjectUtil;
import com.zero.common.constants.CodeConstants;

import java.util.HashMap;

/*
*   响应结果类
*       返回一个 Map 集合
* */
public class Result extends HashMap<String,Object> {

    // 序列化版本
    private static final long serialVersionUID = 1L;

    // 状态码
    public static final String CODE = "code";

    // 响应的消息
    public static final String MSG = "msg";

    // 响应数据
    public static final String DATA = "data";

    /*
    *   msg
    * */
    public Result(String msg){
        super.put(CODE,CodeConstants.SUCCESS);
        super.put(MSG,msg);
    }

    /*
    *   code msg
    * */
    public Result(int code, String msg){
        super.put(CODE,code);
        super.put(MSG,msg);
    }

    /*
    *   code msg data
    * */
    public Result(int code, String msg, Object data){
        super.put(CODE,code);
        super.put(MSG,msg);
        if (!ObjectUtil.isEmpty(data)) { // 对返回的对象数据，进行判空处理
            super.put(DATA,data);
        }
    }

    /*
    *   操作成功
    * */
    public static Result success(){
        return new Result(CodeConstants.SUCCESS,"操作成功");
    }

    /*
    *   操作成功   msg
    * */
    public static Result success(String msg){
        return new Result(CodeConstants.SUCCESS,msg);
    }

    /*
     *   操作成功   msg  data
     * */
    public static Result success(String msg, Object data){
        return new Result(CodeConstants.SUCCESS,msg,data);
    }

    /*
     *   操作失败    code:500   msg
     * */
    public static Result error(String msg){
        return new Result(CodeConstants.ERROR,msg);
    }

    /*
     *   操作失败    自定义code   msg
     * */
    public static Result error(int code, String msg){
        return new Result(code,msg);
    }

}
